package cn.btks.resultintegration.rule.service.impl;

import cn.btks.commonserver.pojo.PageList;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.resultintegration.rule.dto.rule.YswrwhdlzbQueryParam;
import cn.btks.resultintegration.rule.pojo.Yswrwhdlzb;
import cn.btks.resultintegration.rule.pojo.Yswrwhdlzb;
import cn.btks.resultintegration.rule.repository.YswrwhdlzbRepository;
import cn.btks.resultintegration.rule.service.YswrwhdlzbService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author lijun
 * @CompileUnit 测绘地理信息分院研发中心
 * @Date 2023/9/22 14:38
 */
@Service
public class YswrwhdlzbServiceImpl implements YswrwhdlzbService {

    @Autowired
    YswrwhdlzbRepository yswrwhdlzbRepository;

    @Override
    public List<Yswrwhdlzb> findAll() {
        return yswrwhdlzbRepository.findAll();
    }

    @Override
    public ResultState findPages(YswrwhdlzbQueryParam param) {
        PageRequest pageRequest = PageRequest.of(param.getPageNumber() - 1, param.getPageSize());
        Specification<Yswrwhdlzb> specification = new Specification<Yswrwhdlzb>() {
            @Override
            public Predicate toPredicate(Root<Yswrwhdlzb> root,
                                         CriteriaQuery<?> criteriaQuery,
                                         CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(StringUtils.isNotBlank(param.getSm())){
                    predicates.add(criteriaBuilder.equal(root.get("sm"), param.getSm()));
                }
                if(StringUtils.isNotBlank(param.getFl())){
                    predicates.add(criteriaBuilder.equal(root.get("fl"), param.getFl()));
                }
                if(StringUtils.isNotBlank(param.getWrw())){
                    predicates.add(criteriaBuilder.equal(root.get("wrw"), param.getWrw()));
                }
                if(StringUtils.isNotBlank(param.getXh())){
                    predicates.add(criteriaBuilder.equal(root.get("xh"), param.getXh()));
                }
                Predicate[] predicates1 = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(predicates1));
            }
        };
        Page<Yswrwhdlzb> configPage = yswrwhdlzbRepository.findAll(specification, pageRequest);
        if (configPage.getTotalElements() > 0) {
            PageList pageList = new PageList();
            pageList.setResult(configPage.getContent());
            pageList.setPageNum(configPage.getTotalPages());
            pageList.setTotalCount(configPage.getTotalElements());
            return new ResultState(StateInfo.FIND_OK.getCode(), StateInfo.FIND_OK.getMsg(), pageList);
        }
        return new ResultState(StateInfo.FIND_NULL.getCode(), StateInfo.FIND_NULL.getMsg());
    }
}
